<%--
 *  File:    AuthorityEntity.jsp
 *  @Author: Shine zhen 
 *  Date:    2001/11/19
 *
 *  Copyright (C) 2000 - 2001 Oval Technologies, Inc.
 *  All rights reserved. 
 *
 *  This software is the confidential and proprietary information of Oval 
 *  Technologies, Inc. ("Confidential Information").  You shall not
 *  disclose such Confidential Information and shall use it only in
 *  accordance with the terms of the license agreement you entered into
 *  with Oval.
--%>


<%@ include file="/inc/SessionTimeout.inc"%>
<%@ page language="java" %>     
<%@ page import="java.util.*" %>
<%@ page import="org.apache.log4j.Category" %>
<%@ page import="com.oval.util.*" %>

<%@ page contentType = "text/html;charset=UTF-8" %>
<%@ taglib uri="/WEB-INF/PageFlow.tld" prefix="PageFlow" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/custom.tld" prefix="custom" %>

<%
  //init param
  Category cat = Category.getInstance("jsp.AuthorityEntity"); 
  String strModuleName = "UPM";
  String privilegeCode = "Z0203,L404";
%>



<%  
  Enumeration enum2 = request.getParameterNames();
  while(enum2.hasMoreElements())
  {
    String paramName = (String)enum2.nextElement();
    cat.info("XXXXXXXXXXXXXXXX ELEM IS " + paramName);
  }
  String strLanguageCode = (String)session.getAttribute("languagecode");
  if(strLanguageCode == null)
  {
    strLanguageCode = request.getParameter("languagecode");
    if(strLanguageCode == null)
       strLanguageCode = "cn";
  }
 
  OvalLabel label = new OvalLabel(strLanguageCode,strModuleName);  
  OvalCustomMessage message = new OvalCustomMessage(strLanguageCode);  
  
   String entitytype = request.getParameter("theentitytype");
   String entityid = (String)session.getAttribute("theentityid");
   if(entitytype != null)
   {
      cat.info("enter if ");
      if(entitytype.equals("p"))
      {
        cat.info("confirm the entitytype is p");
        entityid = request.getParameter("thepccode");
      }
      else
      {
        if((entitytype.equals("so"))||(entitytype.equals("to")))
        {
          entityid = request.getParameter("theorgid");
        }
        else
        {
          if(entitytype.equals("g"))
          {
            entityid = request.getParameter("thegroupid");
          }
          else
          {
            if(entitytype.equals("u"))
            {
              entityid = request.getParameter("theuserid");   
            }
            else
              cat.info("error");
          }
        }
      }    
   }
   if(entityid != null)
   {
    session.setAttribute("theentityid",entityid);
   }
   cat.info("the entityid = " + entityid);
   if(entitytype != null)
      session.setAttribute("theentitytype",entitytype);
      
   String strthepccode = request.getParameter("thepccode");
   if(strthepccode != null)
   {
      Integer thepccode = new Integer(strthepccode);
      session.setAttribute("thepccode",thepccode);
   }
   
   String strtheorgid = request.getParameter("theorgid");
   if(strtheorgid != null)
   {
      Integer theorgid = new Integer(strtheorgid);
      session.setAttribute("theorgid",theorgid);
   }
      
   String strthegroupid = request.getParameter("thegroupid");
   if(strthegroupid != null)
   {
      Integer thegroupid = new Integer(strthegroupid);
      session.setAttribute("thegroupid",thegroupid);
   }  
   
   String strtheuserid = request.getParameter("theuserid");
   if(strtheuserid != null)
   {
      Integer theuserid = new Integer(strtheuserid);
      session.setAttribute("theuserid",theuserid);
   }
   
   entitytype = (String)session.getAttribute("theentitytype");
   cat.info("the entitytype is "+ entitytype);
%>
<%@ include file="/inc/Security.inc" %>


<html>
<head>
<title>Authority</title>
<PageFlow:CheckPageFlow pageName="authority"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="../../css/<%= strLanguageCode %>/main.css" type="text/css">
<script language="JavaScript" src="../../js/FuncLib.js"></script>
</head>

<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<table width="100%" cellspacing="0" cellpadding="0">
  <tr> 
    <td class="summary_top"></td>
  </tr>
</table>
<table width="100%" cellspacing="0" cellpadding="3">
  <tr> 
    <td> 
      <table width="100%" border="1" cellpadding="0" bordercolorlight="#999999" bordercolordark="#FFFFFF" cellspacing="0" align="center">
        <tr> 
          <td width="40%"height="60" class="deepblue-center" onclick="allChecked(grantprivlist)" ondblclick="allCancel(grantprivlist)"><%= label.getLabelName("L_PrivAvailable") %></td>
          <td class="deepblue-center">&nbsp;</td>
          <td width="40%" class="deepblue-center"><%= label.getLabelName("L_PrivAssigned") %> </td>
        </tr>
        <tr> 
          <td valign="top" > 
            
            <%
              Vector orgprofiledetail = (Vector)pageContext.getAttribute("orgprofiledetail");
              if(orgprofiledetail == null)
              {
                orgprofiledetail = new Vector();
              }
              cat.info("the orgprofiledetail is " + orgprofiledetail);
              
              Vector otherprivlist = (Vector)pageContext.getAttribute("otherprivlist");
              cat.info("the otherprivlist is " + otherprivlist);
              if(otherprivlist == null)
              {
                otherprivlist = new Vector();
              }
              
              Vector authorityprivlist = (Vector)pageContext.getAttribute("authorityprivlist");
              cat.info("the authorityprivlist is " + authorityprivlist);
              if(authorityprivlist == null)
              {
                authorityprivlist = new Vector();
              }
              if(entitytype.equals("to")||entitytype.equals("so"))
              {
                entitytype = "O";
              }
            %>
            <table width="100%" cellspacing="0" cellpadding="0" >
            <form name="grantprivlist" method="post" action="../../Dispatcher">
            <input type="hidden" name="pagename" value="authority">
            <input type="hidden" name="actionname" value="grant">
            <input type="hidden" name="entitytype" value="<%=JspHelper.filterNull(entitytype.toUpperCase())%>">
            <input type="hidden" name="entityid" value="<%=JspHelper.filterNull(entityid)%>">
            <input type="hidden" name="datelastupdated" value="<%=JspHelper.filterNull( new java.sql.Date(new java.util.Date().getTime()) )%>">
            <span id="pv" style="display:none"><input type="hidden" name="numrows" value="0"></span>
            <%
              int grantcount = 0;
              int length = orgprofiledetail.size();
              if(length > 0)
              {
            %>
            <SCRIPT language=JavaScript src="../../css/menu.js"></SCRIPT>
            <%
              }
              grantcount = length;
              int count = 1;
              Enumeration e_orgprofiledetail = orgprofiledetail.elements();
              if(e_orgprofiledetail.hasMoreElements())
              {
                HashMap orgprofile = (HashMap)e_orgprofiledetail.nextElement();
                if(orgprofile == null)
                {
                  orgprofile = new HashMap();
                }
                boolean first = true;  
                while(e_orgprofiledetail.hasMoreElements()||first)
                {
                  first = false;
                  String privprofname = (String)orgprofile.get("privprofname");
                  if(privprofname == null)
                  {
                    privprofname = "";
                  }
            %>
              <tr> 
                <td class="tr-left-row3"><a href="javascript:expandIt('KB0<%=JspHelper.filterNull(count)%>'); return false"><img name="KB0<%=JspHelper.filterNull(count)%>wj" src="../../images/<%= strLanguageCode %>/TreeAdd.gif" width="11" height="11" border="0" ></a> 
                  <input type="checkbox" name="<%=JspHelper.filterNull(privprofname)%>" value="checkbox" class="normalradio" onClick="clickProfile(this)">
                  <%=JspHelper.filterNull( privprofname )%><div class=child id=KB0<%=JspHelper.filterNull(count)%>Child> 
                    <table cellspacing="0" width="100%">
                    <%
                      String profname = privprofname;
                      while(profname.equals(privprofname))
                      {
                        
                    %>
                      <tr> 
                        <td class="tr-left-row4"><img src="../../images/<%= strLanguageCode %>/TreeSub.gif" width="11" height="11" > 
                          <input type="checkbox" name="grantprivlistids" sourceid="<%=JspHelper.filterNull( orgprofile.get("authorityid") )%>" value="<%=JspHelper.filterNull( orgprofile.get("privlistid") )%>" class="normalradio">
                          <%=JspHelper.filterNull( orgprofile.get("privlistname"))%></td>
                      </tr>
                    <%
                        if(e_orgprofiledetail.hasMoreElements())
                        {
                          orgprofile = (HashMap)e_orgprofiledetail.nextElement();
                          if(orgprofile == null)
                          {
                            orgprofile = new HashMap();
                          }
                          profname = (String)orgprofile.get("privprofname");
                          if(profname == null)
                          {
                            profname = "";
                          }
                        }
                        else
                          break;
                      }
                      count++;
                    %>
                    </table>
                  </div></td>
              </tr>
              <%
              }
              }
             /**
              the otherprivlist begin to deal
              */
              length = otherprivlist.size();
              grantcount = grantcount + length;
              count = 0;
              
              cat.info("the grantcount is " + grantcount);
              for(int i = orgprofiledetail.size(); i < grantcount; i++)
              {
                HashMap otherpriv = (HashMap)otherprivlist.elementAt(count);
                count++;
                if(otherpriv == null)
                {
                  otherpriv = new HashMap();
                }
              %>
              <tr> 
                <td class="td-listrom" > 
                  <input type="checkbox" name="grantprivlistids" sourceid="<%=JspHelper.filterNull( otherpriv.get("authorityid") )%>" value="<%=JspHelper.filterNull( otherpriv.get("privlistid") )%>" class="checkbox">
                  <%=JspHelper.filterNull( otherpriv.get("privlistname"))%></td>
              </tr>
              <%
              }
              %>
            </form>
              </table>
          </td>
          <td height="98"> 
            <table width="100%" cellspacing="0" cellpadding="0">
              <tr> 
                <td class="td_center"><a onclick="grant()"><custom:button type="1" label="L_Confer" imgsrc="confer.gif" border="0" style="width:60"/></a> 
                </td>
              </tr>
              <tr> 
                <td>&nbsp;</td>
              </tr>
              <tr> 
                <td class="td_center"><a onclick="revoke1()"><custom:button type="1" label="L_Cancel" imgsrc="cancel.gif" border="0" style="width:60"/></a> 
                </td>
              </tr>
            </table>
          </td>
          <form name="revoke" method="post" action="../../Dispatcher">
          <input type="hidden" name="pagename" value="authority">
          <input type="hidden" name="actionname" value="revoke">
          <input type="hidden" name="entitytype" value="<%=JspHelper.filterNull(entitytype.toUpperCase())%>">
          <input type="hidden" name="entityid" value="<%=JspHelper.filterNull(entityid)%>">
          <td> 
            <select name="revokeauthorityids" size="20" style="width:240" multiple>
            <%
              length = authorityprivlist.size();
              for(int i = 0; i < length; i++)
              {
                HashMap authoritypriv = (HashMap)authorityprivlist.elementAt(i);
                if(authoritypriv == null)
                {
                  authoritypriv = new HashMap();
                }
             %>
             <option name="authorityid<%=JspHelper.filterNull( i )%>" value="<%=JspHelper.filterNull( authoritypriv.get("authorityid") )%>" ><%=JspHelper.filterNull( authoritypriv.get("privlistname") )%></option>
            <%
              }
            %>
            </select>
          </td>
          </form>
          </tr>
        <tr> 
          <td class="td-listrom" colspan="3">&nbsp;</td>
        </tr>
      </table>
    </td>
  </tr>
</table>

<script language="JavaScript">
  var databuffer = new Array();
  <%
    length = orgprofiledetail.size();
    for(int i = 0; i < length; i++)
    {
  %>
      databuffer[<%=JspHelper.filterNull(i)%>] = "<%=JspHelper.filterNull(((HashMap)orgprofiledetail.elementAt(i)).get("privprofname"))%>";
  <%
    }
  %>
  var hasGranted=false;
  function grant()
  {
    //alert("1:"+hasGranted);
    if(hasGranted)
      return;
    else
      hasGranted=true;  
   //alert("2:"+hasGranted);   
  <%
    if(grantcount > 1)
    {
  %>
    var hasselect = false;       
    var i;
    var j;
    var rowcount = 0;
    for(i = 0; i < <%=JspHelper.filterNull( grantcount )%>; i++)
    {
      if(document.grantprivlist.grantprivlistids[i].checked == true && !document.grantprivlist.grantprivlistids[i].disabled)
      {
        pv.innerHTML += "<input type='hidden' name='privlistid" + rowcount + "' value='" + document.grantprivlist.grantprivlistids[i].value + "'>";
        pv.innerHTML += "<input type='hidden' name='sourceauthorityid" + rowcount + "' value='" + document.grantprivlist.grantprivlistids[i].sourceid + "'>";
        rowcount ++;
        hasselect = true;
      }  
      for(j = 0; j < i; j++)
      {
        if((document.grantprivlist.grantprivlistids[i].checked == true)&&
          (document.grantprivlist.grantprivlistids[i].value == document.grantprivlist.grantprivlistids[j].value)&&
          (document.grantprivlist.grantprivlistids[j].checked == true))
          {
            document.grantprivlist.grantprivlistids[i].disabled = true;
          }
        
      }
    }
    document.grantprivlist.numrows.value = rowcount;
    <%--[SCRIPT]  alert(pv.innerHTML);  [SCRIPT]--%>
    if(hasselect)
    {
      isRunning = true;
      document.grantprivlist.submit();
    }
    else
    {
      hasGranted=false;
      alert("<%= message.getMessageDesc("PLEASE_SELECT") %>");
    }
   <%
    }
    else if(grantcount == 1)
    {
   %>
    if(document.grantprivlist.grantprivlistids.checked)
    {
      pv.innerHTML += "<input type='hidden' name='privlistid0' value='" + document.grantprivlist.grantprivlistids.value + "'>";
      pv.innerHTML += "<input type='hidden' name='sourceauthorityid0' value='" + document.grantprivlist.grantprivlistids.sourceid + "'>";
      document.grantprivlist.numrows.value = 1;
      <%--[SCRIPT]  alert(pv.innerHTML);  [SCRIPT]--%>
      isRunning = true;
      document.grantprivlist.submit();
    }
    else
    {
      hasGranted=false;
      alert("<%= message.getMessageDesc("PLEASE_SELECT") %>");
    }
   <%
    }
   %>
   
  }
  function revoke1()
  {
    <%
    length = authorityprivlist.size();
    %>
    var flag = false;
    for(i = 0; i < <%=JspHelper.filterNull(length)%>; i++)
      if(document.revoke.revokeauthorityids.options[i].selected == true)
      {
        flag = true;
      } 
    if(flag)
    {
      if(confirm("<%= message.getMessageDesc("ARE_YOU_SURE_DEL") %>"))
      {
        isRunning = true;
        document.revoke.submit();
      }
    }
    else
      alert("<%= message.getMessageDesc("PLEASE_SELECT") %>");
 }
  function clickProfile(box)
  {
  <%
    if(grantcount > 1)
    {
    length = orgprofiledetail.size();
  %>
    for(i = 0; i < <%=JspHelper.filterNull( length )%>; i++)
    {
      if(databuffer[i] == box.name)
        document.grantprivlist.grantprivlistids[i].checked = box.checked;
    }
  <%
    }
    else
    {
  %>
    document.grantprivlist.grantprivlistids.checked = box.checked;
  <%
    }
  %>
  }
</script>

</body>
</html>


